﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Core.Models;

namespace Core.Controllers
{
    public class ThemeController : Controller
    {
        //
        // GET: /Theme/

        public ActionResult Index()
        {
            return View();
        }

        //
        // POST: /TCList/

        [HttpPost]
        public JsonResult ThemeList(FormCollection collection)
        {
            CoreEntities core = new CoreEntities();
            int SkipRecs = int.Parse(collection.GetValues("iDisplayStart")[0].ToString());
            int TakeCount = int.Parse(collection.GetValues("iDisplayLength")[0].ToString());
            string strSearch = collection.GetValues("sSearch")[0].ToString();
            DataTableResponse r = new DataTableResponse();
            // Needs update when paging & sorting is implemented
            List<Theme> list = core.Themes.OrderBy(m => m.Name).Skip(SkipRecs).Take(TakeCount).ToList();
            r.sEcho = collection.GetValues("sEcho")[0].ToString();
            // This query is required because the earlier query returns results for only 1 page.
            r.iTotalRecords = core.Themes.Count();
            r.iTotalDisplayRecords = r.iTotalRecords;
            foreach (Theme tc in list)
            {
                List<string> r1 = new List<string>();
                string editurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"editlink\" title=\"{1}\" >Edit</a>", "Theme", "Edit", tc.ThemeId.ToString());
                string detailsurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"detailslink\" title=\"{1}\" >Details</a>", "Theme", "Details", tc.ThemeId.ToString());
                string deleteurl = String.Format("<a href=\"/{0}/{1}/{2}\" id=\"deletelink\" title=\"{1}\" >Delete</a>", "Theme", "Delete", tc.ThemeId.ToString());
                r1.Add(editurl + detailsurl + deleteurl);
                r1.Add(tc.Name);
                r1.Add(tc.IsForMobileYN == "Y" ? "Yes" : "No");
                r1.Add(tc.Width == 0 ? "Fluid" : tc.Width.ToString());
/*
                r1.Add(tc.SysMenuIdentifier);
                r1.Add(tc.MenuIdentifier);
                r1.Add(tc.ContentIdentifier); */
                r.aaData.Add(r1); 
            }
            return Json(r);
        }

        //
        // GET: /Theme/Details/5

        public ActionResult Details(Guid id)
        {
            CoreEntities core = new CoreEntities();
            Theme theme = core.Themes.First(m => m.ThemeId == id);
            return View(theme);
        }

        //
        // GET: /Theme/Create

        public ActionResult Create()
        {
            Theme theme = new Theme();
            return View(theme);
        } 

        //
        // POST: /Theme/Create

        [HttpPost]
        public ActionResult Create(Theme theme)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CoreEntities core = new CoreEntities();
                    theme.ThemeId = Guid.NewGuid();
                    core.AddToThemes(theme);
                    core.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                    return View(theme);
            }
            catch (Exception e)
            {
                Tools.DebugInfo(Tools.ExceptionMessage(e));
                ModelState.AddModelError("", Tools.ExceptionMessage(e));
                return View(theme);
            }
        }
        
        //
        // GET: /Theme/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            CoreEntities core = new CoreEntities();
            Theme theme = core.Themes.First(m => m.ThemeId == id);
            return View(theme);
        }

        //
        // POST: /Theme/Edit/5

        [HttpPost]
        public ActionResult Edit(Theme theme, FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    CoreEntities core = new CoreEntities();
                    Theme t = core.Themes.First(m => m.ThemeId == theme.ThemeId);
                    UpdateModel(t, collection.ToValueProvider());
                    core.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                    return View(theme);
            }
            catch (Exception e)
            {
                Tools.DebugInfo(Tools.ExceptionMessage(e));
                ModelState.AddModelError("", Tools.ExceptionMessage(e));
                return View(theme);
            }
        }

        //
        // GET: /Theme/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            CoreEntities core = new CoreEntities();
            Theme theme = core.Themes.First(m => m.ThemeId == id);
            return View(theme);
        }

        //
        // POST: /Theme/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid ThemeId, FormCollection collection)
        {
            try
            {
                CoreEntities core = new CoreEntities();
                core.Themes.DeleteObject(core.Themes.First(m => m.ThemeId == ThemeId));
                core.SaveChanges();
                return RedirectToAction("Index");
            }
            catch(Exception e)
            {
                Tools.DebugInfo(Tools.ExceptionMessage(e));
                ModelState.AddModelError("", Tools.ExceptionMessage(e));
                CoreEntities core = new CoreEntities();
                Theme theme = core.Themes.First(m => m.ThemeId == ThemeId);
                return View(theme);
            }
        }
    }
}
